"use strict";

import {
    //核心
    React, Enhance, Controller, Component, $, EventEmitter,
    //
    tools, classnames,
    // 组件
    Table, TabPage, Lang, Dialog, Tree,
    Form, Page, Upload, Icon, Grid,
    BoxWidget, Input, Button, Prompt, Link, Loaders, Editor, Resource, Directive, Lock, Fix
} from 'admin';

import debug from 'debug';
import Cars from '../widget/w-cars';
import Fittingtype from '../widget/w-fittingtype';
import Fitting from  '../widget/w-fitting';

var {http, date, time} = tools;
const log = debug('ysw.fitting');

/**
 *
 */
class FittingController extends Controller {
    state = {
        status: [
            {title: '开启', value: 1},
            {title: '关闭', value: 0}
        ],
        //unit: [
        //    {title: 10201008, value: 10201008},
        //    {title: 10201009, value: 10201009},
        //],
        table1: {
            update: {},
            query: {},
            types: {},
            onRowClick: this._handlerRowClick,
            buttons: {
                'edit': {
                    onClick: this._handlerEditDialog
                }
            },
            pk: 'id',
            fields: [
                {
                    title: "#",
                    width: 1,
                    type: {
                        name: "#"
                    }
                },
                {
                    order: -1,
                    name: 'id',
                    width: 1,
                    display: false
                },
                {
                    order: 0,
                    name: 'code'
                },
                {
                    name: 'name',
                    order: 0,
                },
                {
                    order: 0,
                    name: 'origin'
                },
                {
                    name: 'model',
                    order: 0,
                },
                {
                    order: 0,
                    name: 'unit'
                },
                {
                    name: 'w_price',
                    order: 0,
                },
                {
                    order: 0,
                    name: 'p_price'
                },
                {
                    name: 'o_price',
                    order: 0,
                },
                {
                    order: 0,
                    name: 'l_price'
                },
                {
                    name: 'serie',
                    order: 0,
                },
                {
                    order: 0,
                    name: 'type'
                },
                {
                    name: 'use',
                    order: 0,
                },
                {
                    order: 0,
                    name: 'min'
                },
                {
                    name: 'pos_code',
                    order: 0,
                },
                {
                    order: 0,
                    name: 'content'
                },
                {
                    name: 'num',
                    order: 0,
                },
                {
                    name: 'locat',
                    order: 0,
                }

            ]
        },
        row: {},
        edit: {},
        saveButton: null,
        updateButton: null
    }
    
    /**
     * 在componentWillMount()在挂载发生之前立即被调用。
     */
    componentWillMount() {
    
    }
    
    /**
     *
     */
    constructor() {
        super();
    }
    
    render() {
        var {edit, row} = this.state;
        var config = {
            height: 568,
            heightMax: 568,
            width: 320,
        }
        
        return (
            <Grid>
                <Grid col="12 12 12 12">
                    {this.renderTable1()}
                </Grid>


                <Fitting ref="fitting" onAddSuccess={this._handlerAddSuccess} onEditSuccess={this._handlerEditSuccess}></Fitting>
                <Cars ref="cars" onCache={() => { this.forceUpdate() }}></Cars>
                <Fittingtype ref="fittingtype" onCache={() => { this.forceUpdate() }}></Fittingtype>
            </Grid>
        );
    }

    /**
     *
     * @return {*}
     */
    renderTable1() {
        return (
            <BoxWidget
                color="3"
                className="table-height scroll"
            >
                <div data-fix="buttons">
                    <Button onClick={this._handlerAddSerieDialog}>
                        <Icon icon="add"></Icon>
                        <Lang lang="add_serie"></Lang>
                    </Button>
                    <Button onClick={this._handlerAddTypeDialog}>
                        <Icon icon="add"></Icon>
                        <Lang lang="add_type"></Lang>
                    </Button>
                    <Button onClick={this._handlerAddDialog}>
                        <Icon icon="add"></Icon>
                        <Lang lang="ysw.fitting.add"></Lang>
                    </Button>
                </div>
                <div data-fix="content">
                    <Lock>
                        <Table ref="table" {...this.props} {...this.state.table1}/>
                    </Lock>
                </div>
            </BoxWidget>
        );
    }

    _handlerAddSuccess(data) {
        if (data) {
            this.refs.table.append(data);
        }
    }

    /**
     *
     * @param update
     * @private
     */
    _handlerEditSuccess(data) {
        if (data) {
            this.refs.table.update(data);
        }
    }
    
    /**
     *
     * @private
     */
    _handlerAddDialog() {
        this.refs.fitting.showAddDialog();
    }

    _handlerAddSerieDialog(){
        this.refs.cars.showAddDialog();
    }

    _handlerAddTypeDialog(){
        this.refs.fittingtype.showAddDialog();
    }
    
    /**
     *
     * @param edit
     * @private
     */
    _handlerEditDialog(edit) {
        this.refs.fitting.showEditDialog(edit)
    }
    
    /**
     * 表单查询
     * @param data
     * @returns {boolean}
     * @private
     */
    _handlerQuery(data) {
        this.refs.table.query(data);
        return false;
    }
    
}

export default Enhance(FittingController);
